package com.lottery.im.module.main.presenter;

import android.app.Activity;
import com.lottery.im.MyApplication;
import com.lottery.im.api.ApiClient;
import com.lottery.im.api.observer.BaseObserver;
import com.lottery.im.base.BaseActivityPresenter;
import com.lottery.im.module.main.view.VerPwdInputView;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import java.util.concurrent.TimeUnit;

public class VerPwdInputPresenter extends BaseActivityPresenter<VerPwdInputView> {

  public VerPwdInputPresenter(Activity activity,
      VerPwdInputView iView,
      LifecycleProvider<ActivityEvent> provider) {
    super(activity, iView, provider);
  }

  /**
   * 校验验证密码
   *
   * @param pwd 密码
   */
  public void checkEquipmentPwd(String pwd) {
    ApiClient.getApiRetrofitInstance()
        .checkEquipmentPwd(MyApplication.getInstance().getUserInfo().getUserId(),
            MyApplication.getInstance().getToken(), pwd)
        .throttleFirst(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new BaseObserver<Object>() {
          @Override public void onStart() {
            iView.showLoadingDialog();
          }

          @Override public void onSuccess(Object object) {
            iView.cancelLoadingDialog();
            iView.checkEquipmentPwdSuce();
          }

          @Override public void onFail(String code, String msg) {
            iView.cancelLoadingDialog();
            iView.showToast(msg);
            iView.checkEquipmentPwdError();
          }

          @Override public void onException(String msg) {
            iView.cancelLoadingDialog();
            iView.showError();
            iView.checkEquipmentPwdError();
          }

          @Override public void onFinish() {

          }
        });
  }
}
